package luccy.behavior;

import luccy.attribute.AttributeContainer;
import luccy.attribute.Session;
import luccy.container.ContainerAware;
import luccy.container.ExternalAware;
import luccy.container.Registry;

public class SessionBehavior implements RuntimeBehavior, ContainerAware, ExternalAware {

	private Registry container;

	private Session session;

	public SessionBehavior(Registry container) {
	}

	public <T> T get(Class<? super T> clazz) {
		Object attribute = session.getAttribute(clazz.getName());
		if (attribute == null) {
			T t = container.getComponent(clazz);
			session.setAttribute(clazz.getName(), t);
			return t;
		} else {
			return null;
		}
	}

	public void setCointainer(Registry container) {
		this.container = container;
	}

	public void setAttributeContainer(AttributeContainer attributeContainer) {
		if (attributeContainer instanceof Session) {
			this.session = (Session) attributeContainer;
		}
	}

}
